package CCRMClient.GUI;

import java.awt.LayoutManager;
import java.awt.SystemColor;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;













import javax.swing.JOptionPane;














import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.*;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ClientSaleManager;
import Common.SERVER_COMMANDS;
import Common.Entities.*;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import net.proteanit.sql.DbUtils;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;



/**
 * This class implements the sale initiation UC
 * @author ori ziv
 *
 */
public class SaleInitiationScreen extends JPanel {
	private JLabel lblSaleInitiation;
	private JLabel lblAvailableTemplates;
	private JScrollPane scpTemplates;
	private JTable tblTemplates;
	private JLabel lblSelectATemplate;
	private JLabel lblSaleName;
	private JLabel lblSaleDescription;
	private JLabel lblSaleStart;
	private JLabel lblStartYear;
	private JLabel lblStartMonth;
	private JLabel lblStartDay;
	private JTextField textSaleName;
	private JTextField textSaleDesc;
	private JTextField textSMonth;
	private JTextField textSYear;
	private JTextField textSDay;
	private JLabel lblSaleFinish;
	private JLabel lblFinishYear;
	private JLabel lblFinishMonth;
	private JLabel lblFinishDay;
	private JTextField textFYear;
	private JTextField textFMonth;
	private JTextField textFDay;
	private JButton btnSubmitNewSale;
	private JButton btnGoBack;

	public SaleInitiationScreen() {
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		initComponents();
	}
	public SaleInitiationScreen(ResultSet approved) {
		this();
		loadRStoTable(approved);
	}
	
	private void loadRStoTable(ResultSet approved) {
		tblTemplates.setModel(DbUtils.resultSetToTableModel(approved));
		
		JTableHeader th = tblTemplates.getTableHeader();
		TableColumnModel tcm = th.getColumnModel();
		TableColumn tc = tcm.getColumn(0);
		tc.setHeaderValue("ID");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(1);
		tc.setHeaderValue("Name");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(2);
		tc.setHeaderValue("Description");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(3);
		tc.setHeaderValue("Message");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(4);
		tc.setHeaderValue("Product ID");
		
		th = tblTemplates.getTableHeader();
		tcm = th.getColumnModel();
		tc = tcm.getColumn(4);
		tc.setHeaderValue("Template Status");
		
		tblTemplates.getColumnModel().getColumn(0).setPreferredWidth(29);
		tblTemplates.getColumnModel().getColumn(0).setMaxWidth(50);
		tblTemplates.getColumnModel().getColumn(1).setPreferredWidth(83);
		tblTemplates.getColumnModel().getColumn(2).setPreferredWidth(159);
		tblTemplates.getColumnModel().getColumn(3).setPreferredWidth(286);
		tblTemplates.getColumnModel().getColumn(4).setPreferredWidth(57);
		
		th.repaint();
	}
	
	private void initComponents() {
		setSize (1029, 601);
		setBackground(Color.WHITE);
		setLayout(null);
		
		lblSaleInitiation = new JLabel("Sale Initiation");
		lblSaleInitiation.setForeground(Color.RED);
		lblSaleInitiation.setFont(new Font("David", Font.BOLD, 28));
		lblSaleInitiation.setHorizontalAlignment(SwingConstants.CENTER);
		lblSaleInitiation.setBounds(0, 11, 1029, 45);
		add(lblSaleInitiation);
		add(getLblAvailableTemplates());
		add(getScpTemplates());
		add(getLblSelectATemplate());
		add(getLblSaleName());
		add(getLblSaleDescription());
		add(getLblSaleStart());
		add(getLblStartYear());
		add(getLblStartMonth());
		add(getLblStartDay());
		add(getTextSaleName());
		add(getTextSaleDesc());
		add(getTextSMonth());
		add(getTextSYear());
		add(getTextSDay());
		add(getLblSaleFinish());
		add(getLblFinishYear());
		add(getLblFinishMonth());
		add(getLblFinishDay());
		add(getTextFYear());
		add(getTextFMonth());
		add(getTextFDay());
		add(getBtnSubmitNewSale());
		add(getBtnGoBack());
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{getTblTemplates(), getTextSaleName(), getTextSaleDesc(), getTextSYear(), getTextSMonth(), getTextSDay(), getTextFYear(), getTextFMonth(), getTextFDay(), getBtnSubmitNewSale(), getBtnGoBack()}));
	}
	private JLabel getLblAvailableTemplates() {
		if (lblAvailableTemplates == null) {
			lblAvailableTemplates = new JLabel("Available Templates:");
			lblAvailableTemplates.setHorizontalAlignment(SwingConstants.LEFT);
			lblAvailableTemplates.setForeground(Color.RED);
			lblAvailableTemplates.setFont(new Font("David", Font.BOLD, 22));
			lblAvailableTemplates.setBounds(10, 53, 224, 27);
		}
		return lblAvailableTemplates;
	}
	private JScrollPane getScpTemplates() {
		if (scpTemplates == null) {
			scpTemplates = new JScrollPane();
			scpTemplates.setViewportBorder(null);
			scpTemplates.setBounds(10, 91, 986, 121);
			scpTemplates.setViewportView(getTblTemplates());
		}
		return scpTemplates;
	}
	private JTable getTblTemplates() {
		if (tblTemplates == null) {
			tblTemplates = new JTable();
			tblTemplates.setBackground(SystemColor.menu);
			tblTemplates.setFont(new Font("David", Font.BOLD, 11));
			tblTemplates.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblTemplates.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
			tblTemplates.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null, null, null},
				},
				new String[] {
					"ID", "Name", "Description", "Message", "Status"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false, false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
			tblTemplates.getColumnModel().getColumn(0).setPreferredWidth(29);
			tblTemplates.getColumnModel().getColumn(0).setMaxWidth(50);
			tblTemplates.getColumnModel().getColumn(1).setPreferredWidth(83);
			tblTemplates.getColumnModel().getColumn(2).setPreferredWidth(159);
			tblTemplates.getColumnModel().getColumn(3).setPreferredWidth(286);
			tblTemplates.getColumnModel().getColumn(4).setPreferredWidth(57);
		}
		return tblTemplates;
	}
	private JLabel getLblSelectATemplate() {
		if (lblSelectATemplate == null) {
			lblSelectATemplate = new JLabel("Select a template and fill the details below:");
			lblSelectATemplate.setHorizontalAlignment(SwingConstants.LEFT);
			lblSelectATemplate.setForeground(Color.RED);
			lblSelectATemplate.setFont(new Font("David", Font.BOLD, 22));
			lblSelectATemplate.setBounds(10, 223, 986, 27);
		}
		return lblSelectATemplate;
	}
	private JLabel getLblSaleName() {
		if (lblSaleName == null) {
			lblSaleName = new JLabel("Sale name:");
			lblSaleName.setHorizontalAlignment(SwingConstants.LEFT);
			lblSaleName.setForeground(Color.BLACK);
			lblSaleName.setFont(new Font("David", Font.BOLD, 22));
			lblSaleName.setBounds(10, 261, 224, 27);
		}
		return lblSaleName;
	}
	private JLabel getLblSaleDescription() {
		if (lblSaleDescription == null) {
			lblSaleDescription = new JLabel("Sale Description:");
			lblSaleDescription.setHorizontalAlignment(SwingConstants.LEFT);
			lblSaleDescription.setForeground(Color.BLACK);
			lblSaleDescription.setFont(new Font("David", Font.BOLD, 22));
			lblSaleDescription.setBounds(10, 299, 224, 27);
		}
		return lblSaleDescription;
	}
	private JLabel getLblSaleStart() {
		if (lblSaleStart == null) {
			lblSaleStart = new JLabel("Sale Start:");
			lblSaleStart.setHorizontalAlignment(SwingConstants.LEFT);
			lblSaleStart.setForeground(Color.RED);
			lblSaleStart.setFont(new Font("David", Font.BOLD, 22));
			lblSaleStart.setBounds(10, 337, 119, 27);
		}
		return lblSaleStart;
	}
	private JLabel getLblStartYear() {
		if (lblStartYear == null) {
			lblStartYear = new JLabel("Start year:");
			lblStartYear.setHorizontalAlignment(SwingConstants.LEFT);
			lblStartYear.setForeground(Color.BLACK);
			lblStartYear.setFont(new Font("David", Font.BOLD, 22));
			lblStartYear.setBounds(10, 375, 119, 27);
		}
		return lblStartYear;
	}
	private JLabel getLblStartMonth() {
		if (lblStartMonth == null) {
			lblStartMonth = new JLabel("Start month:");
			lblStartMonth.setHorizontalAlignment(SwingConstants.LEFT);
			lblStartMonth.setForeground(Color.BLACK);
			lblStartMonth.setFont(new Font("David", Font.BOLD, 22));
			lblStartMonth.setBounds(10, 413, 135, 27);
		}
		return lblStartMonth;
	}
	private JLabel getLblStartDay() {
		if (lblStartDay == null) {
			lblStartDay = new JLabel("Start day:");
			lblStartDay.setHorizontalAlignment(SwingConstants.LEFT);
			lblStartDay.setForeground(Color.BLACK);
			lblStartDay.setFont(new Font("David", Font.BOLD, 22));
			lblStartDay.setBounds(10, 451, 135, 27);
		}
		return lblStartDay;
	}
	private JTextField getTextSaleName() {
		if (textSaleName == null) {
			textSaleName = new JTextField();
			textSaleName.setColumns(10);
			textSaleName.setBackground(SystemColor.menu);
			textSaleName.setBounds(191, 266, 251, 20);
		}
		return textSaleName;
	}
	private JTextField getTextSaleDesc() {
		if (textSaleDesc == null) {
			textSaleDesc = new JTextField();
			textSaleDesc.setColumns(10);
			textSaleDesc.setBackground(SystemColor.menu);
			textSaleDesc.setBounds(191, 304, 251, 20);
		}
		return textSaleDesc;
	}
	private JTextField getTextSMonth() {
		if (textSMonth == null) {
			textSMonth = new JTextField();
			textSMonth.setColumns(10);
			textSMonth.setBackground(SystemColor.menu);
			textSMonth.setBounds(144, 418, 126, 20);
		}
		return textSMonth;
	}
	private JTextField getTextSYear() {
		if (textSYear == null) {
			textSYear = new JTextField();
			textSYear.setColumns(10);
			textSYear.setBackground(SystemColor.menu);
			textSYear.setBounds(144, 380, 126, 20);
		}
		return textSYear;
	}
	private JTextField getTextSDay() {
		if (textSDay == null) {
			textSDay = new JTextField();
			textSDay.setColumns(10);
			textSDay.setBackground(SystemColor.menu);
			textSDay.setBounds(144, 456, 126, 20);
		}
		return textSDay;
	}
	private JLabel getLblSaleFinish() {
		if (lblSaleFinish == null) {
			lblSaleFinish = new JLabel("Sale Finish:");
			lblSaleFinish.setHorizontalAlignment(SwingConstants.LEFT);
			lblSaleFinish.setForeground(Color.RED);
			lblSaleFinish.setFont(new Font("David", Font.BOLD, 22));
			lblSaleFinish.setBounds(339, 337, 119, 27);
		}
		return lblSaleFinish;
	}
	private JLabel getLblFinishYear() {
		if (lblFinishYear == null) {
			lblFinishYear = new JLabel("Finish year:");
			lblFinishYear.setHorizontalAlignment(SwingConstants.LEFT);
			lblFinishYear.setForeground(Color.BLACK);
			lblFinishYear.setFont(new Font("David", Font.BOLD, 22));
			lblFinishYear.setBounds(339, 375, 119, 27);
		}
		return lblFinishYear;
	}
	private JLabel getLblFinishMonth() {
		if (lblFinishMonth == null) {
			lblFinishMonth = new JLabel("Finish month:");
			lblFinishMonth.setHorizontalAlignment(SwingConstants.LEFT);
			lblFinishMonth.setForeground(Color.BLACK);
			lblFinishMonth.setFont(new Font("David", Font.BOLD, 22));
			lblFinishMonth.setBounds(339, 413, 135, 27);
		}
		return lblFinishMonth;
	}
	private JLabel getLblFinishDay() {
		if (lblFinishDay == null) {
			lblFinishDay = new JLabel("Finish day:");
			lblFinishDay.setHorizontalAlignment(SwingConstants.LEFT);
			lblFinishDay.setForeground(Color.BLACK);
			lblFinishDay.setFont(new Font("David", Font.BOLD, 22));
			lblFinishDay.setBounds(339, 451, 135, 27);
		}
		return lblFinishDay;
	}
	private JTextField getTextFYear() {
		if (textFYear == null) {
			textFYear = new JTextField();
			textFYear.setColumns(10);
			textFYear.setBackground(SystemColor.menu);
			textFYear.setBounds(473, 380, 126, 20);
		}
		return textFYear;
	}
	private JTextField getTextFMonth() {
		if (textFMonth == null) {
			textFMonth = new JTextField();
			textFMonth.setColumns(10);
			textFMonth.setBackground(SystemColor.menu);
			textFMonth.setBounds(473, 418, 126, 20);
		}
		return textFMonth;
	}
	private JTextField getTextFDay() {
		if (textFDay == null) {
			textFDay = new JTextField();
			textFDay.setColumns(10);
			textFDay.setBackground(SystemColor.menu);
			textFDay.setBounds(473, 456, 126, 20);
		}
		return textFDay;
	}
	private JButton getBtnSubmitNewSale() {
		if (btnSubmitNewSale == null) {
			btnSubmitNewSale = new JButton("Submit New Sale");
			btnSubmitNewSale.addActionListener(new BtnSubmitNewSaleActionListener());
			btnSubmitNewSale.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSubmitNewSale.setBounds(10, 503, 195, 23);
		}
		return btnSubmitNewSale;
	}
	private JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(404, 504, 195, 23);
		}
		return btnGoBack;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new MarketingManagementScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing Submit New Sale button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSubmitNewSaleActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int selected = tblTemplates.getSelectedRow();
			if (selected == -1)
				JOptionPane.showMessageDialog(null, "Please select template", "Sale Initiation", JOptionPane.INFORMATION_MESSAGE);
			else {
				DefaultTableModel dtm = (DefaultTableModel) tblTemplates.getModel();
				String templateID = Long.toString((Long) dtm.getValueAt(selected, 0));
				String saleName = textSaleName.getText();
				String saleDesc = textSaleDesc.getText();
				String sYear = textSYear.getText();
				String sMonth = textSMonth.getText();
				String sDay = textFDay.getText();
				String fYear = textFYear.getText();
				String fMonth = textFMonth.getText();
				String fDay = textFDay.getText();
				if (saleName.isEmpty() || saleDesc.isEmpty() || sYear.isEmpty() || sMonth.isEmpty() || sDay.isEmpty() || fYear.isEmpty() || fMonth.isEmpty() || fDay.isEmpty())
					throw new NullPointerException();
				try {
					int year = Integer.parseInt(sYear);
					int month = Integer.parseInt(sMonth);
					int day = Integer.parseInt(sDay);
					Calendar start = Calendar.getInstance();
					start.set(year, month - 1, day);
					year = Integer.parseInt(fYear);
					month = Integer.parseInt(fMonth);
					day = Integer.parseInt(fDay);
					Calendar finish = Calendar.getInstance();
					finish.set(year, month - 1, day);
					Calendar today = Calendar.getInstance();
					if (start.getTimeInMillis() < today.getTimeInMillis()) throw new NullPointerException();
					if (start.getTimeInMillis() >= finish.getTimeInMillis()) throw new NullPointerException();
					Sale sale = new Sale(-1, saleName, saleDesc, start.getTime(), finish.getTime(), Integer.parseInt(templateID));
					try {
						ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_SALE, ClientSaleManager.addSale(SERVER_COMMANDS.ADD_SALE, sale));
					} catch (IOException e1) {
						JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
						ClientStart.quitApp();
					}
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "Invalid date field", "Sale Initiation", JOptionPane.INFORMATION_MESSAGE);
				} catch (NullPointerException e1) {
					JOptionPane.showMessageDialog(null, "Invalid or missing input", "Sale Initiation", JOptionPane.INFORMATION_MESSAGE);
				}
				
				
				
				
			}
		}
	}
}
	







	